﻿Public Class Form1

    Public Function GetFileTypes() As List(Of String)
        Using key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot
            Dim keys As New List(Of String)
            keys.AddRange(key.GetSubKeyNames)
            Dim ext As IEnumerable(Of String) = _
            From s In keys Where s.StartsWith(".")

            Return ext.ToList



        End Using
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
    End Sub
    Private Shared icons As New List(Of Bitmap)


    Private Sub GetIconsSTA()
        SyncLock icons
            icons.Clear()
            For Each ext As String In GetFileTypes()
                icons.Add(FileIcon.GetIconAsImageFromExtension(ext, FileIcon.enmIconSize.Large, ""))
                icons(icons.Count - 1).MakeTransparent()

            Next
        End SyncLock
        If Me.InvokeRequired Then
            Me.Invoke(New Done(AddressOf DoneSTA))
        Else
            DoneSTA()
        End If


    End Sub
    Delegate Sub Done()
    Private Sub DoneSTA()
        lvIcons.LargeImageList = Nothing
        SyncLock icons
            ilIcons.Images.Clear()
            ilIcons.Images.AddRange(icons.ToArray)
        End SyncLock
        Dim keys As List(Of String) = GetFileTypes()

        For x As Integer = 0 To keys.Count - 1
            ilIcons.Images.SetKeyName(x, keys(x))
        Next

        lvIcons.Invalidate()
        
        lvIcons.LargeImageList = ilIcons
        'lvIcons.Items.Clear()
        If lvIcons.Items.Count = 0 Then
            For Each ext As String In keys
                Dim li As New ListViewItem(ext, ext)
                li.Tag = ext
                lvIcons.Items.Add(li)

            Next
        End If

        SetEnabled(True)

    End Sub
    Private Sub btnSTA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSTA.Click
        SetEnabled(False)
        Dim th As New Threading.Thread(AddressOf GetIconsSTA)
        th.IsBackground = True
        th.SetApartmentState(Threading.ApartmentState.STA)
        th.Start()


    End Sub

   

    Private Sub bgwIcons_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwIcons.DoWork
        GetIconsSTA()
    End Sub

    Private Sub btnbgw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbgw.Click
        SetEnabled(False)
        bgwIcons.RunWorkerAsync()

    End Sub
    Private Sub SetEnabled(ByVal Enabled As Boolean)
        btnbgw.Enabled = Enabled
        btnSTA.Enabled = Enabled
        btnComponent.Enabled = Enabled

    End Sub
    
    Private Sub IconList_GenerateIconsComplete(ByVal Sender As Object, ByVal e As FileIconListCreator.GeneratiionDoneEventArgs) Handles IconList.GenerateIconsComplete
       
        

        ComponnentDone(e)

    End Sub
    Delegate Sub ComponentDoneDelegate(ByVal e As FileIconListCreator.GeneratiionDoneEventArgs)
    Private Sub ComponnentDone(ByVal e As FileIconListCreator.GeneratiionDoneEventArgs)
        If Me.InvokeRequired Then
            Dim cd As New ComponentDoneDelegate(AddressOf ComponnentDone)
            Me.Invoke(cd, New Object() {e})
        Else
            
            lvIcons.LargeImageList = Nothing
            Dim ic As IEnumerable(Of Bitmap) = _
       From i In e.Infos Select i.Icon
            ilIcons.Images.Clear()
            ilIcons.Images.AddRange(ic.ToArray)
            lvIcons.LargeImageList = Me.ilIcons
            If lvIcons.Items.Count = 0 Then
                For x As Integer = 0 To e.Infos.Count - 1
                    Dim info As FileInfo = e.Infos(x)
                    ilIcons.Images.SetKeyName(x, info.FileName)

                    Dim li As New ListViewItem(info.FileName, info.FileName)
                    li.ToolTipText = info.FileTypeDescription

                    lvIcons.Items.Add(li)

                Next
            End If


            'lvIcons.Invalidate()
            'lvIcons.Items.Clear()


            SetEnabled(True)

        End If
    End Sub
    Private Sub btnComponent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComponent.Click
        SetEnabled(False)

        IconList.Files = GetFileTypes()
        IconList.Generate()

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        lvIcons.Items.Clear()
        ilIcons.Images.Clear()

    End Sub
    
End Class
